package org.springframework.web.servlet.ajax.json;

import java.io.PrintWriter;
import java.util.Locale;

/**
 * This PrintWriter is simply acting as a proxy to a StringBuffer.  The purpose is to
 * gather information about what the PrintWriter is writing and then to write that information
 * ourselves to a HttpServletResponse PrintWriter.   If this simple solution
 * doesn't work, we could record each method call and argument, and then repeat them
 * on a real PrintWriter.  But that's probably overkill for our simple purpose.
 * 
 * @author Charles Tassoni
*/

public class HoldsContentPrintWriter extends PrintWriter {

	private StringBuffer sb;

	public HoldsContentPrintWriter(PrintWriter printWriter, StringBuffer buf){
		//We need the printWriter because our subclass doesn't have an empty-arg
		//constructor.  But we don't need to hold onto it as delegate
		super(printWriter);
		this.sb = buf;
	}

	@Override
	public PrintWriter append(char c) {
		sb.append(c);
		return this;
	}

	@Override
	public PrintWriter append(CharSequence csq, int start, int end) {
		sb.append(csq, start, end);
		return this;
	}

	@Override
	public PrintWriter append(CharSequence csq) {
		sb.append(csq);
		return this;
	}

	@Override
	public boolean checkError() {
		throw new UnsupportedOperationException();
	}

	@Override
	public void close() {
		//We don't want the delegate closed during the time we're gathering
		//information about what it's writing, because it's not really writing!
	}

	@Override
	public void flush() {
	}

	@Override
	public PrintWriter format(Locale l, String format, Object... args) {
		throw new UnsupportedOperationException();
	}

	@Override
	public PrintWriter format(String format, Object... args) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void print(boolean b) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void print(char c) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void print(char[] s) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void print(double d) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void print(float f) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void print(int i) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void print(long l) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void print(Object obj) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void print(String s) {
		throw new UnsupportedOperationException();
	}

	@Override
	public PrintWriter printf(Locale l, String format, Object... args) {
		throw new UnsupportedOperationException();
	}

	@Override
	public PrintWriter printf(String format, Object... args) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void println() {
		throw new UnsupportedOperationException();
	}

	@Override
	public void println(boolean x) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void println(char x) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void println(char[] x) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void println(double x) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void println(float x) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void println(int x) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void println(long x) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void println(Object x) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void println(String x) {
		throw new UnsupportedOperationException();
	}

	@Override
	protected void setError() {
		throw new RuntimeException("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@set Error called!!");
	}

	@Override
	public void write(char[] buf, int off, int len) {
		this.sb.append(buf, off, len);
	}

	@Override
	public void write(char[] buf) {
		this.sb.append(buf);
	}

	@Override
	public void write(int c) {
		this.sb.append(c);
	}

	@Override
	public void write(String s, int off, int len) {
		this.sb.append(s, off, len);
	}

	@Override
	public void write(String s) {
		this.sb.append(s);
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		throw new CloneNotSupportedException();
	}

	@Override
	public boolean equals(Object obj) {
		return false;
	}

	@Override
	protected void finalize() throws Throwable {
		super.finalize();
	}

	@Override
	public int hashCode() {
		return sb.hashCode();
	}

	@Override
	public String toString() {
		return sb.toString();
	}



}
